package network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import protocol.ProtocolEngine;

/**
 * @author Peter Nikodem
 */
public class ClientListener extends Thread {

    private final ProtocolEngine protocol;
    private final Socket socket;
    private BufferedReader in;

    ClientListener(Socket socket, ProtocolEngine protocol) {
        this.protocol = protocol;
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (!interrupted()) {
                String message = in.readLine();
                if (message == null) {
                    break;
                }
                protocol.processMessage(message);
            }
        } catch (IOException ioe) {
//            log
        }
        protocol.connectionInterrupted();
    }
}
